package kd.bos.openapi.sdk.response;

import kd.bos.openapi.sdk.response.result.ResultCode;
import kd.bos.openapi.sdk.util.PropertyUtil;
import kd.bos.openapi.sdk.util.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
 * @Deacription 请求响应ResponseSaveDto/data对象，该对象主要目的是对service层返回结果统一封装后返回给controller，
 * 可以根据各自业务需要更改，如果不需要封装一层，可以直接返回Controller
 * @author johnz
 * @date 2022年4月6日
 */
public class APIResponseDto<T, E> {
    private String thirdApp;
    private String code;
    private String message;
    private String trace;
    private String signature;
    private E encryptInfo;
    private T data;//返回业务数据/json字符串

    // 成功返回
    public boolean isSuccess() {
        if (!StringUtils.isEmpty(code) && NumberUtils.isNumber(code)) {
            return Integer.parseInt(code) == 0;
        }
        return false;
    }

    public static APIResponseDto createRequest() {
        String thirdApp = PropertyUtil.getProperty("bos.appId");
        APIResponseDto requestQuery = new APIResponseDto(thirdApp, null, null, StringUtils.getTrace(thirdApp));
        return requestQuery;
    }

    public static APIResponseDto createRequest(String thirdApp, String code, String message, String trace){
        APIResponseDto requestQuery = new APIResponseDto(thirdApp, code, message,trace);
        return requestQuery;
    }

    public static APIResponseDto createRequest(String thirdApp, ResultCode resultCode, String trace){
        return createRequest(thirdApp, resultCode.getCode()+"", resultCode.getMessage(),trace);
    }

    public static APIResponseDto createRequest(String thirdApp, ResultCode resultCode){
        return createRequest(thirdApp, resultCode.getCode()+"", resultCode.getMessage(),StringUtils.getTrace(thirdApp));
    }
    public static APIResponseDto createRequestByMessage(ResultCode resultCode, String message){
        String thirdApp = PropertyUtil.getProperty("bos.appId");
        return createRequest(thirdApp, resultCode.getCode()+"", resultCode.getMessage()+message,StringUtils.getTrace(thirdApp));
    }

    public static APIResponseDto createRequest(ResultCode resultCode){
        String thirdApp = PropertyUtil.getProperty("bos.appId");
        return createRequest(thirdApp, resultCode.getCode()+"", resultCode.getMessage(),StringUtils.getTrace(thirdApp));
    }

    public APIResponseDto of(T data) {
        this.data = data;
        return this;
    }

    public APIResponseDto(String thirdApp, String code, String message, String trace) {
        this.code = code;
        this.message = message;
        this.trace = trace;
        this.thirdApp=thirdApp;
    }

    public APIResponseDto(String thirdApp, String code, String message, String trace, E encryptInfo, T data) {
        this.code = code;
        this.message = message;
        this.trace = trace;
        this.encryptInfo = encryptInfo;
        this.data = data;
        this.thirdApp=thirdApp;
    }

    public E getEncryptInfo() {
        return encryptInfo;
    }

    public void setEncryptInfo(E encryptInfo) {
        this.encryptInfo = encryptInfo;
    }

    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getTrace() {
        return trace;
    }

    public void setTrace(String trace) {
        this.trace = trace;
    }

    public String getThirdApp() {
        return thirdApp;
    }

    public void setThirdApp(String thirdApp) {
        this.thirdApp = thirdApp;
    }
}
